home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / segment.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  36KB  |  1,271 lines

  1.  
  2.    /*********************************************
  3.    *
  4.    *       file d:\cips\segment.c
  5.    *
  6.    *       Functions: This file contains
  7.    *           adaptive_threshold_segmentation
  8.    *           append_stack_files
  9.    *           copy_stack_files
  10.    *           find_peaks
  11.    *           find_valley_point
  12.    *           grow
  13.    *           insert_into_peaks
  14.    *           insert_into_deltas
  15.    *           label_and_check_neighbors
  16.    *           manual_threshold_segmentation
  17.    *           peak_threshold_segmentation
  18.    *           peaks_high_low
  19.    *           push_data_onto_stack_file
  20.    *           pop_data_off_of_stack_file
  21.    *           threshold_image_array
  22.    *           valley_high_low
  23.    *           valley_threshold_segmentation
  24.    *           get_segmentation_options
  25.    *
  26.    *       Purpose:
  27.    *           These functions are part of histogram
  28.    *           based image segmentation.
  29.    *
  30.    *       External Calls:
  31.    *          wtiff.c - round_off_image_size
  32.    *                    create_file_if_needed
  33.    *                    write_array_into_tiff_image
  34.    *          tiff.c - read_tiff_header
  35.    *          rtiff.c - read_tiff_image
  36.    *          numcvrt.c - get_integer
  37.    *                      get_short
  38.    *
  39.    *       Modifications:
  40.    *           October 1992 - created
  41.    *
  42.    ************************************************/
  43.  
  44.  
  45. #include "cips.h"
  46.  
  47.  
  48.    /**************************************************
  49.    *
  50.    *   manual_threshold_segmentation(...
  51.    *
  52.    *   This function segments an image using thresholding
  53.    *   given the hi and low values of the threshold
  54.    *   by the calling routine.  It reads in an image
  55.    *   and writes the result to the output image.
  56.    *
  57.    *   If the segment parameter is 0, you only
  58.    *   threshold the array - you do not segment.
  59.    *
  60.    ***************************************************/
  61.  
  62. manual_threshold_segmentation(in_name, out_name,
  63.                               the_image, out_image,
  64.                               il, ie, ll, le,
  65.                               hi, low, value, segment)
  66.    char   in_name[], out_name[];
  67.    int    il, ie, ll, le, segment;
  68.    short  hi, low, the_image[ROWS][COLS],
  69.           out_image[ROWS][COLS], value;
  70. {
  71.    int    length, width;
  72.    struct tiff_header_struct image_header;
  73.  
  74.    create_file_if_needed(in_name, out_name, out_image);
  75.  
  76.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  77.    threshold_image_array(the_image, out_image,
  78.             hi, low, value);
  79.    if(segment == 1)
  80.       grow(out_image, value);
  81.    write_array_into_tiff_image(out_name, out_image,
  82.                                il, ie, ll, le);
  83.  
  84. }  /* ends manual_threshold_segmentation */
  85.  
  86.  
  87.  
  88.  
  89.  
  90.    /************************************************
  91.    *
  92.    *   peak_threshold_segmentation(...
  93.    *
  94.    *   This function segments an image using
  95.    *   thresholding.  It uses the histogram peaks
  96.    *   to find the hi and low values of the
  97.    *   threshold.
  98.    *
  99.    *   If the segment parameter is 0, you only
  100.    *   threshold the array - you do not segment.
  101.    *
  102.    *************************************************/
  103.  
  104. peak_threshold_segmentation(in_name, out_name,
  105.                             the_image, out_image,
  106.                             il, ie, ll, le,
  107.                             value, segment)
  108.    char   in_name[], out_name[];
  109.    int    il, ie, ll, le, segment;
  110.    short  the_image[ROWS][COLS],
  111.           out_image[ROWS][COLS], value;
  112. {
  113.    int      length, peak1, peak2, width;
  114.    short    hi, low;
  115.    struct   tiff_header_struct image_header;
  116.    unsigned long histogram[GRAY_LEVELS+1];
  117.  
  118.    create_file_if_needed(in_name, out_name, out_image);
  119.  
  120.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  121.    zero_histogram(histogram);
  122.    calculate_histogram(the_image, histogram);
  123.    smooth_histogram(histogram);
  124.    find_peaks(histogram, &peak1, &peak2);
  125.    peaks_high_low(histogram, peak1, peak2,
  126.                   &hi, &low);
  127.    threshold_image_array(the_image, out_image,
  128.                          hi, low, value);
  129.    if(segment == 1)
  130.       grow(out_image, value);
  131.    write_array_into_tiff_image(out_name, out_image,
  132.                                il, ie, ll, le);
  133.  
  134. }  /* ends peak_threshold_segmentation */
  135.  
  136.  
  137.  
  138.  
  139.  
  140.    /************************************************
  141.    *
  142.    *   valley_threshold_segmentation(...
  143.    *
  144.    *   This function segments an image using
  145.    *   thresholding.  It uses the histogram valleys
  146.    *   to find the hi and low values of the
  147.    *   threshold.
  148.    *
  149.    *   If the segment parameter is 0, you only
  150.    *   threshold the array - you do not segment.
  151.    *
  152.    *************************************************/
  153.  
  154. valley_threshold_segmentation(in_name, out_name,
  155.                               the_image, out_image,
  156.                               il, ie, ll, le,
  157.                               value, segment)
  158.    char   in_name[], out_name[];
  159.    int    il, ie, ll, le, segment;
  160.    short  the_image[ROWS][COLS],
  161.           out_image[ROWS][COLS], value;
  162. {
  163.    int      length, peak1, peak2, width;
  164.    short    hi, low;
  165.    struct   tiff_header_struct image_header;
  166.    unsigned long histogram[GRAY_LEVELS+1];
  167.  
  168.    create_file_if_needed(in_name, out_name, out_image);
  169.  
  170.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  171.    zero_histogram(histogram);
  172.    calculate_histogram(the_image, histogram);
  173.    smooth_histogram(histogram);
  174.    find_peaks(histogram, &peak1, &peak2);
  175.    valley_high_low(histogram, peak1, peak2,
  176.                    &hi, &low);
  177.    threshold_image_array(the_image, out_image,
  178.                          hi, low, value);
  179.    if(segment == 1)
  180.       grow(out_image, value);
  181.    write_array_into_tiff_image(out_name, out_image,
  182.                                il, ie, ll, le);
  183.  
  184. }  /* ends valley_threshold_segmentation */
  185.  
  186.  
  187.  
  188.  
  189.  
  190.    /************************************************
  191.    *
  192.    *   adaptive_threshold_segmentation(...
  193.    *
  194.    *   This function segments an image using
  195.    *   thresholding.  It uses two passes
  196.    *   to find the hi and low values of the
  197.    *   threshold.  The first pass uses the peaks
  198.    *   of the histogram to find the hi and low
  199.    *   threshold values.  It thresholds the image
  200.    *   using these hi lows and calculates the means
  201.    *   of the object and background.  Then we use
  202.    *   these means as new peaks to calculate new
  203.    *   hi and low values.  Finally, we threshold
  204.    *   the image again using these second hi low
  205.    *   hi low values.
  206.    *
  207.    *   If the segment parameter is 0, you only
  208.    *   threshold the array - you do not segment.
  209.    *
  210.    *************************************************/
  211.  
  212. adaptive_threshold_segmentation(in_name, out_name,
  213.                                 the_image, out_image,
  214.                                 il, ie, ll, le,
  215.                                 value, segment)
  216.    char   in_name[], out_name[];
  217.    int    il, ie, ll, le, segment;
  218.    short  the_image[ROWS][COLS],
  219.           out_image[ROWS][COLS], value;
  220. {
  221.    int      length, peak1, peak2, width;
  222.    short    background, hi, low, object;
  223.    struct   tiff_header_struct image_header;
  224.    unsigned long histogram[GRAY_LEVELS+1];
  225.  
  226.    create_file_if_needed(in_name, out_name, out_image);
  227.  
  228.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  229.    zero_histogram(histogram);
  230.    calculate_histogram(the_image, histogram);
  231.    smooth_histogram(histogram);
  232.    find_peaks(histogram, &peak1, &peak2);
  233.    peaks_high_low(histogram, peak1, peak2,
  234.                   &hi, &low);
  235.    threshold_and_find_means(the_image, out_image,
  236.                             hi, low, value,
  237.                             &object, &background);
  238.    peaks_high_low(histogram, object, background,
  239.                   &hi, &low);
  240.    threshold_image_array(the_image, out_image,
  241.                          hi, low, value);
  242.    if(segment == 1)
  243.       grow(out_image, value);
  244.    write_array_into_tiff_image(out_name, out_image,
  245.                                il, ie, ll, le);
  246.  
  247. }  /* ends adaptive_threshold_segmentation */
  248.  
  249.  
  250.  
  251.  
  252.  
  253.    /***************